<template>
	<Layout style="padding:0 24px 24px;height:100%;">
		<Breadcrumb style="margin:24px 0">
			<BreadcrumbItem>首页</BreadcrumbItem>
			<BreadcrumbItem>角色管理</BreadcrumbItem>
		</Breadcrumb>
		<Form inline>
			<ButtonGroup style="margin-right:24px;">
				<Button title="新增" icon="md-add" @click="editModal = true"></Button>
				<Button title="删除" :disabled="selection.length == 0" icon="md-trash" @click="removeBatch"></Button>
			</ButtonGroup>
			<FormItem><Input type="text" placeholder="角色编号" clearable v-model="pageRequest.condition.roleCode" /></FormItem>
			<FormItem><Input type="text" placeholder="角色名称" clearable v-model="pageRequest.condition.roleName" /></FormItem>
			<FormItem><Button type="primary" icon="ios-search" shape="circle" title="查询" @click="list"></Button></FormItem>
		</Form>
		<Content ref="content" style="padding:0px;background:#fff;height:100%">
			<Table :loading="loading" :columns="tableColumns" :data="pageData.content" :height="tableHeight" @on-selection-change="onSelectionChange" border>
				<template slot-scope="{ row, index }" slot="action">
					<Button type="warning" size="small" style="margin-right: 5px" @click="modify(row)">修改</Button>
					<Button type="error" size="small" style="margin-right: 5px" @click="remove(row)">删除</Button>
					<Button type="primary" size="small" @click="openAuthModal(row)">权限分配</Button>
				</template>
				<template slot="footer">
					<Page style="margin-left:10px;" :total="pageData.total" :current="pageData.page" :page-size="15" @on-change="changePage"></Page>
				</template>
			</Table>
		</Content>
		<!-- 编辑对话框 -->
		<Modal :width="400" v-model="editModal" @on-visible-change="onVisibleChange" title="编辑角色信息" footer-hide>
			<Form ref="roleForm" :model="roleInfo" :label-width="80" :rules="roleRules">
				<FormItem label="角色代码" prop="roleCode"><Input v-model="roleInfo.roleCode" type="text" placeholder="请输入角色代码" /></FormItem>
				<FormItem label="角色名称" prop="roleName"><Input v-model="roleInfo.roleName" type="text" placeholder="请输入角色名称" /></FormItem>
				<FormItem label="角色别名" prop="aliasName"><Input v-model="roleInfo.aliasName" type="text" placeholder="请输入角色别名" /></FormItem>
				<FormItem label="附加信息" prop="addInfo"><Input v-model="roleInfo.addInfo" type="text" placeholder="请输入附加信息" /></FormItem>
				<FormItem label="描述信息" prop="roleDesc"><Input v-model="roleInfo.roleDesc" type="text" placeholder="请输入描述信息" /></FormItem>
				<FormItem label="显示顺序" prop="sequence"><Input v-model="roleInfo.sequence" type="number" placeholder="请输入序号" /></FormItem>
				<FormItem><Button type="primary" @click="save">保存</Button></FormItem>
			</Form>
		</Modal>
		<!-- 权限分配对话框-->
		<Modal :width="400" v-model="authModal" title="权限分配" footer-hide>
			<div class="auth-list-content">
				<Select v-model="authSelection" multiple>
					<Option v-for="item in authList" :value="item.id" :key="item.id">{{ item.authName }}</Option>
				</Select>
			</div>
			<Button type="primary" @click="saveRoleAuth">确定</Button>
			<Button type="warning" @click="authModal = false" style="margin-left:5px;">取消</Button>
		</Modal>
	</Layout>
</template>

<script language="JavaScript">
export default {
	components: {},
	data() {
		return {
			tableHeight: 100, // 自适应表格高度
			loading: false, // 加载中
			tableColumns: [
				{
					type: 'selection',
					width: 80
				},
				{
					title: '角色代码',
					key: 'roleCode',
					width: 'auto',
					minWidth: 250,
					resizable: true
				},
				{
					title: '角色名称',
					key: 'roleName',
					width: 250,
					resizable: true
				},
				{
					title: '角色别名',
					key: 'aliasName',
					width: 250,
					resizable: true
				},
				{
					title: '附加信息',
					key: 'addInfo',
					width: 250,
					resizable: true
				},
				{
					title: '角色描述',
					key: 'roleDesc',
					width: 250,
					resizable: true
				},
				{
					title: '序号',
					key: 'sequence',
					width: 100,
					resizable: true
				},
				{
					title: '操作',
					slot: 'action',
					fixed: 'right',
					width: 250
				}
			], // 表格列
			// 分页请求
			pageRequest: {
				page: 1,
				rows: 15,
				sort: 'sequence:asc',
				condition: {}
			},
			// 分页查询结果
			pageData: {
				page: 1,
				total: 2,
				content: [] // 表格数据
			},
			editModal: false, // 编辑对话框显示
			roleInfo: {
				roleCode: '',
				roleName: '',
				aliasName: '',
				addInfo: '',
				roleDesc: '',
				sequence: 0
			}, // 保存信息
			roleRules: {
				roleCode: [
					{
						required: true,
						message: '必填'
					}
				],
				roleName: [
					{
						required: true,
						message: '必填'
					}
				],
				sequence: [
					{
						required: true,
						message: '必填'
					}
				]
			}, // 验证规则
			selection: [], //已选记录
			authModal: false, // 权限分配对话框
			authList: [], // 权限列表
			authSelection: [], // 选中权限列表
			currentRoleInfo: {} // 当前选中的角色行
		};
	},
	computed: {},
	watch: {
		'roleInfo.roleCode': function() {
			this.roleInfo.roleCode = this.roleInfo.roleCode.toUpperCase();
		}
	},
	created: function() {},
	mounted: function() {
		this.$nextTick(() => {
			this.tableHeight = this.$refs.content.$el.clientHeight;
			this.list();
			this.getAuthList();
		});
	},
	methods: {
		// 保存
		save: function() {
			this.$refs['roleForm'].validate(valid => {
				if (valid) {
					this.$request.post(
						'/admin/sys/role/save',
						this.roleInfo,
						res => {
							this.editModal = false;
							this.list();
						},
						true,
						true
					);
				}
			});
		},
		// 获取列表
		list: function() {
			this.$request.post(
				'/admin/sys/role/list',
				this.pageRequest,
				res => {
					this.pageData = res.data || this.pageData;
				},
				false,
				false
			);
		},
		// 修改
		modify: function(row) {
			this.editModal = true;
			this.$nextTick(() => {
				this.roleInfo = Object.assign({}, row);
			});
		},
		// 删除
		remove: function(row) {
			this.$Modal.confirm({
				title: '确认？',
				content: '删除后不可恢复，是否继续？',
				onOk: () => {
					this.$request.post(
						'/admin/sys/role/delete',
						row,
						res => {
							this.list();
						},
						true,
						true
					);
				}
			});
		},
		// 批量删除
		removeBatch: function() {
			let ids = this.$utils.getPropertyArrayFromObjectArray(this.selection, 'id');
			if (!ids || ids.length == 0) {
				this.$Message.warning('请选择角色');
				return;
			}
			this.$Modal.confirm({
				title: '确认？',
				content: '删除后不可恢复，是否继续？',
				onOk: () => {
					this.$request.post(
						'/admin/sys/role/deleteBatch',
						ids,
						res => {
							this.list();
						},
						true,
						true
					);
				}
			});
		},
		// 关闭对话框
		onVisibleChange: function() {
			this.$refs['roleForm'].resetFields();
			this.roleInfo.id = '';
		},
		// 表格多选事件
		onSelectionChange: function(sel) {
			this.selection = sel;
		},
		// 分页
		changePage: function(page) {
			this.pageRequest.page = page;
			this.list();
		},
		// 打开权限分配对话框
		openAuthModal: function(row) {
			this.$request.get(
				`/admin/sys/role-auth/getAuthIds/${row.id}`,
				{},
				res => {
					this.authSelection = res.data || [];
				},
				false,
				false
			);
			this.authModal = true;
			this.currentRoleInfo = row;
		},
		// 保存角色权限关系
		saveRoleAuth: function() {
			let roleAuthInfo = {
				roleId: this.currentRoleInfo.id,
				authIds: this.authSelection
			};
			this.$Modal.confirm({
				title: '确认？',
				content: '确认修改角色权限？',
				onOk: () => {
					this.$request.post(
						'/admin/sys/role-auth/save',
						roleAuthInfo,
						res => {
							this.authModal = false;
						},
						true,
						true
					);
				}
			});
		},
		// 获取列表
		getAuthList: function() {
			this.$request.post(
				'/admin/sys/auth/list',
				{
					page: 1,
					rows: 99999,
					sort: 'sequence:asc'
				},
				res => {
					this.authList = (res.data || {}).content || [];
				},
				false,
				false
			);
		}
	}
};
</script>

<style lang="scss" scoped="scoped">
.auth-list-content {
	height: 450px;
	overflow-y: auto;
}
</style>
